<?php

class App_Form_User_Register extends Zend_Form
{

    public function init()
    {
        parent::init();
        $this->setMethod('post');
        $this->setAction('');
        $this->setOptions(array('class' => 'des4forms'));

        $token = new Zend_Form_Element_Hidden('token');
        $this->addElement($token);

        $email = new Zend_Form_Element_Text('email');
        $email->setRequired()
                ->setLabel('Email:')
                ->setFilters(array(
                                  'StringToLower',
                                  'StringTrim'
                             ))
                ->addValidators(array(new Zend_Validate_EmailAddress(),
                                     new Lib_Form_Validate_DbHasNotRecord('\Catalog\Users', 'email')));
        $email->setAttrib('class', 'text-input');
        $email->setAttrib('placeholder', 'Email');
        $email->setDecorators(array('ViewHelper'));
        $this->addElement($email);

        $nick = new Zend_Form_Element_Text('nick');
        $nick->setRequired()
                ->setLabel('Nick name:')
                ->setAttrib('placeholder', 'Nick')
                ->setDecorators(array('ViewHelper'))
                ->addFilter(new Zend_Filter_StripTags())
                ->addValidator(new Lib_Form_Validate_DbHasNotRecord('\Catalog\Users', 'nick'))
                ->addValidator(new Lib_Form_Validate_Nickname());

        $this->addElement($nick);

        $name = new Zend_Form_Element_Text('name');
        $name->setRequired()
                ->setLabel('Your Name:');
        $name->setAttrib('class', 'text-input');
        $name->setAttrib('placeholder', 'Name');
        $name->setDecorators(array('ViewHelper'));
        $name->addErrorMessage('Please, input your name.')
                ->addFilter(new Zend_Filter_StripTags());
        $this->addElement($name);

        $lastname = new Zend_Form_Element_Text('lastname');
        $lastname->setRequired()
                ->setLabel('Your last name:');
        $lastname->setAttrib('class', 'text-input');
        $lastname->setAttrib('placeholder', 'Last name');
        $lastname->setDecorators(array('ViewHelper'));
        $lastname->addErrorMessage('Please, input your last name.')
                ->addFilter(new Zend_Filter_StripTags());
        $this->addElement($lastname);

        $gendersManager = new \Managers\Genders();
        $genders = $gendersManager->getList();
        $genderElement = new Zend_Form_Element_Select('gender');
        $genderElement->setLabel('Gender:')
                ->setRequired()
                ->setDecorators(array('ViewHelper'));
        foreach ($genders as $gender) {
            $genderElement->addMultiOption($gender['id'], $gender['name']);
        }
        $this->addElement($genderElement);

        $birthdayElement = new Zend_Form_Element_Hidden('birthday');
        $birthdayElement->setRequired()
                ->addValidator(new Zend_Validate_Date());
        $this->addElement($birthdayElement);
        $this->initDateSelector();

        $postal = new Zend_Form_Element_Text('postal');
        $postal->setRequired()
                ->setLabel('Postal code:')
                ->setAttrib('placeholder', 'M4C 2M1')
                ->setDecorators(array('ViewHelper'));
        $this->addElement($postal);

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password:')
                ->setRequired()
                ->addValidators(array(
                                     new Lib_Form_Validate_InputEquals('passwordConfirmation', 'Passwords do not match.'),
                                     new Zend_Validate_StringLength(array('min' => 4, 'max' => 16))
                                ));
        $password->setAttrib('class', 'text-input');
        $password->setAttrib('placeholder', 'Password');
        $password->setDecorators(array('ViewHelper'));
        $password->addErrorMessages(array(
                                         Zend_Validate_StringLength::INVALID => 'Password must be more then 6 symbols and less then 15.',
                                         Lib_Form_Validate_InputEquals::NOT_MATCH => 'Passwords do not match.'
                                    ));
        $this->addElement($password);

        $passwordConfirmation = new Zend_Form_Element_Password('passwordConfirmation');
        $passwordConfirmation->setLabel('Password confirm:')
                ->addValidator(new Lib_Form_Validate_InputEquals('password', 'Passwords do not match.'), true)
                ->setRequired();
        $passwordConfirmation->setAttrib('class', 'text-input');
        $passwordConfirmation->setAttrib('placeholder', 'Password confirmation');
        $passwordConfirmation->setDecorators(array('ViewHelper'));
        $passwordConfirmation->addErrorMessages(array(Lib_Form_Validate_InputEquals::NOT_MATCH => 'Passwords do not match.'));
        $this->addElement($passwordConfirmation);



        //        $imgDir = "../public/images/captcha/";

        // check captcha path is writeable
        //        if (is_writable($imgDir)) {
        //
        //            $captcha = new Zend_Form_Element_Captcha('captcha', array(
        //                                                                     'label' => 'Write symbols from image:',
        //                                                                     'captcha' => 'Image',
        //                                                                     'captchaOptions' => array(
        //                                                                         'captcha' => 'Image',
        //                                                                         'wordLen' => 6,
        //                                                                         'timeout' => 300,
        //                                                                         'imgDir' => $imgDir,
        //                                                                         'imgUrl' => '/images/captcha',
        //                                                                         'suffix' => '.c.png',
        //                                                                         'font' => dirname(APPLICATION_PATH) .
        //                                                                                   "/assets/fonts/Glasten_Bold.ttf",
        //                                                                     )
        //                                                                ));
        //
        //        } else {
        //            $captcha = new Zend_Form_Element_Captcha('captcha', array(
        //                                                                     'label' => "Write symbols from image:",
        //                                                                     'captcha' => 'Figlet',
        //                                                                     'captchaOptions' => array(
        //                                                                         'wordLen' => 6,
        //                                                                         'timeout' => 300,
        //                                                                     ),
        //                                                                ));
        //        }
        //        $captcha->setAttrib('class', 'text-input');
        //        $captcha->setDecorators(array('Captcha'));
        //
        //        $this->addElement($captcha);

        //        $submit = new Zend_Form_Element_Submit('submit', array(
        //                                                                'label' => 'Register'
        //                                                           ));
        //        $submit->setAttrib('class', 'button');
        //        $submit->setDecorators(array('ViewHelper'));
        //        $this->addElement($submit);

        $this->setDecorators(array(array('ViewScript', array('viewScript' => '/forms/user/register.phtml'))));

        return $this;
    }

    private function initDateSelector()
    {
        $day = new Zend_Form_Element_Select('day');
        $day->setRequired();

        for ($i = 1; $i < 32; $i++) {
            $day->addMultiOption($i, $i);
        }
        $this->addElement($day);

        $month = new Zend_Form_Element_Select('month');
        $month->setRequired();
        for ($i = 1; $i < 13; $i++) {
            $name = date('F', mktime(0, 0, 0, $i, 1));
            $month->addMultiOption($i, $name);
        }
        $this->addElement($month);

        $start = 1930;
        $year = new Zend_Form_Element_Select('year');
        $year->setRequired();

        $currentYear = date('Y');
        for ($i = $start; $i < $currentYear; $i++) {
            $year->addMultiOption($i, $i);
        }
        $this->addElement($year);
    }

    public function isValid($values) {
        $date = date('Y-m-d', mktime(null, null, null, $values['month'], $values['day'], $values['year']));
        $values['birthday'] = $date;
        return parent::isValid($values);
    }

}
